package com.iyunwen.datatool.dataparse.impl;

import com.iyunwen.datatool.common.annotations.EventTypeAnnotation;
import com.iyunwen.datatool.common.enums.DataSourceTypeEnum;
import com.iyunwen.datatool.dataparse.DataParse;
import com.iyunwen.datatool.model.dbdo.EventDataDO;
import com.iyunwen.datatool.model.vo.SignalEventVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 事件日志数据格式转换
 * @author: Zhu.Guodong
 * @create: 2020-03-19 19:48
 * @version:
 */
@Slf4j
@Component
@EventTypeAnnotation(eventType = DataSourceTypeEnum.EVENT_LOG)
public class EventLogDataParseImpl implements DataParse<EventDataDO> {
    @Override
    public List<EventDataDO> formatDataToNormalization(List<SignalEventVO> signalEventVOList) {
        List<EventDataDO> result = new ArrayList<>();
        for (SignalEventVO signalEventVO : signalEventVOList) {
            //
            EventDataDO eventDataDO = formatItemData(signalEventVO);

            result.add(eventDataDO);
        }
        return result;
    }

    @Override
    public EventDataDO formatItemData(SignalEventVO signalEventVO) {
        EventDataDO eventDataDO = new EventDataDO();
        eventDataDO.setBackgroundColor(signalEventVO.getBgColour());
        List<String> fieldValues = signalEventVO.getFieldValues();
        for (int tdIndex = 0; tdIndex < fieldValues.size(); tdIndex++) {
            String itemTdStr = fieldValues.get(tdIndex);
            switch (tdIndex) {
                case 0:
                    eventDataDO.setEventRemark(itemTdStr);
                    break;
                case 1:
                    SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd,HH:mm:ss:SSS");
                    try {
                        Date parseDate = sdf.parse(itemTdStr);
                        //Timestamp timestamp = new Timestamp(parseDate.getTime());
                        eventDataDO.setEventTime(parseDate.getTime());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    break;
                case 2:
                    eventDataDO.setEventType(itemTdStr);
                    break;
                case 3:
                    eventDataDO.setMasterOrSlave(itemTdStr);
                    break;
                case 4:
                    eventDataDO.setEventNumber(itemTdStr);
                    break;
                case 5:
                    eventDataDO.setEventSource(itemTdStr);
                    break;
                case 6:
                    eventDataDO.setEventDesc(itemTdStr);
                    break;
                case 7:
                    eventDataDO.setEventLevel(itemTdStr);
                    break;
                case 8:
                    eventDataDO.setEventStatus(itemTdStr);
                    break;
                case 9:
                    eventDataDO.setDuration(itemTdStr);
                    break;
                case 10:
                    eventDataDO.setDeviceDescription(itemTdStr);
                    break;
                case 11:
                    eventDataDO.setOperator(itemTdStr);
                    break;
                case 12:
                    eventDataDO.setConformPerson(itemTdStr);
                    break;
                default:
                    break;
            }
        }
        return eventDataDO;
    }

    @Override
    public List<EventDataDO> sortData(List<EventDataDO> baseNormalizationDataVOList) {
        //排序
        Collections.sort(baseNormalizationDataVOList, new Comparator<EventDataDO>() {

            @Override
            public int compare(EventDataDO o1, EventDataDO o2) {
//                Timestamp eventTime1 = o1.getEventTime();
//                Timestamp eventTime2 = o2.getEventTime();
                Long eventTime1 = o1.getEventTime();
                Long eventTime2 = o2.getEventTime();
                return eventTime1.compareTo(eventTime2);
            }

        });
        return baseNormalizationDataVOList;
    }
}
